{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import re\n",
    "\"abcd\"==\"abcd\"\n",
    "\"adccc\" in \"cccc\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'_21a54'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#字串匹配 返回 匹配的第一个字符位置\n",
    "totalCount = \"asfg gasd_21a54\"\n",
    "index=totalCount.index(\"_2\")\n",
    "totalCount[index:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2154\n"
     ]
    }
   ],
   "source": [
    "#找出数字\n",
    "totalCount = \"asfg gasd_21a54\"\n",
    "totalCount = re.sub(\"\\D\", \"\", totalCount) \n",
    "print(totalCount)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'asfg gasd_2155'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "totalCount = \"asfg gasd_2154\"\n",
    "j=0\n",
    "for i in totalCount:\n",
    "    j+=1\n",
    "    if i==\"_\":\n",
    "        num=totalCount[j:]\n",
    "        if num.isdecimal():\n",
    "            nextNum= int(num)+1\n",
    "            totalCount=totalCount[:j]+str(nextNum)\n",
    "        else:\n",
    "             \"0\"\n",
    "totalCount"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num=u\"12\"\n",
    "if num.isdigit():\n",
    "    num= int(num)+1\n",
    "num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 4, 124, 4]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=[2,4]#(214,5,4,66,30)\n",
    "def cat(c):\n",
    "\n",
    "    c+=(124,4)\n",
    "cat(a)\n",
    "\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcdefgh_izero\n"
     ]
    }
   ],
   "source": [
    "# 去除数字\n",
    "\n",
    "from string import digits\n",
    "\n",
    "s = 'abc123def456gh_i789zero0'\n",
    "remove_digits = str.maketrans('', '', digits)\n",
    "res = s.translate(remove_digits)\n",
    "\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 正则表达"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 替换 查找"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小高jimmy_\n",
      "小高jimmy9999_!!\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "s='小高jimmy9999_!!'\n",
    "res = re.findall('^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[\\u4E00-\\u9FA5])[\\u4E00-\\u9FA5A-Za-z0-9]*$',s)\n",
    "res = re.findall('(\\d)',s)\n",
    "\n",
    "# 用x 替换找到的字符  sub （rule，replace，string）\n",
    "res=re.sub('[0-9]|\\W','',s)\n",
    "print(res)\n",
    "\n",
    "res=re.sub('(?=[0-9])\\W','',s)\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 匹配开始与匹配结束"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'小高0c'"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s=r'小高aajimmy9999_!!\\n[a-zA-Z])(?=.*[\\u4E00aa-\\u9FA5]aas)[\\u4E00-\\u9FA5\\n222aaac'\n",
    "import re\n",
    "a=re.sub(r\"aa.*aa?\",\"0\",s)\n",
    "a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 例子\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 去掉所有空格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "' 草莓2斤 小果 '"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import re\n",
    "b = re.compile(r'  ')\n",
    "\n",
    "b.sub('', ' 草莓2斤 小果 ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
